package com.small.auth.config.service;


import com.small.auth.service.IAuUserService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * 登陆操作
 *
 * @author wangguochao
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private IAuUserService iAuUserService;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        Set<GrantedAuthority> set = new HashSet<>();
        if (authentication != null && "wx-xcx".equals(authentication.getName())) {
            set.add(new SimpleGrantedAuthority("wx-xcx-openid"));
            return new User(username, username, set);
        }
        return iAuUserService.getUserByName(username);
    }
}
